﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace tactics
{
    // If a given type of terrain doesn't appear in the Costs table,
    // it is assumed that the movement type cannot enter that tile.

    // ex. MovementTypes["Standard"].Costs["Open"] = 1
    //     MovementTypes["Flying"].Costs["Impassable"] = error
    using MovementTypes = Dictionary<string, MovementType>;
    using CostIndex = Dictionary<string, int>;

    public struct MovementType
    {
        public CostIndex Costs;
        public string Texture;

        public MovementType(string textureIn, CostIndex costsIn)
        {
            Costs = costsIn;
            Texture = textureIn;
        }
    }

    public class MovementRules
    {
        MovementTypes movementTypes;

        public MovementRules(string xmlFile)
        {
            movementTypes = new MovementTypes();

            Parse(xmlFile);
        }

        private void Parse(string xmlFile)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(xmlFile);

            // check if this is a proper xml file
            XmlNodeList name = doc.GetElementsByTagName("MovementTypes");
            if (name.Count == 0)
                return;

            XmlNodeList types = doc.GetElementsByTagName("Type");

            foreach (XmlNode node in types)
            {
                string moveType = node.Attributes["Name"].Value;
                string texture = node.Attributes["Texture"].Value;
                CostIndex costs = new CostIndex();

                foreach (XmlNode child in node.ChildNodes)
                {
                    string allowType = child.InnerText;
                    int cost = int.Parse(child.Attributes["Cost"].Value);

                    costs[allowType] = cost;
                }

                movementTypes[moveType] = new MovementType(texture, costs);
            }
        }

        public MovementType GetMovementTypeByName(string name)
        {
            if (movementTypes.ContainsKey(name))
                return movementTypes[name];
            else
                return new MovementType();
        }
    }
}
